package com.shycloud.mido.music.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.music.domain.MisMusic;
import com.shycloud.mido.music.domain.MisMusicCollection;
import com.shycloud.mido.music.mapper.MisMusicMapper;
import com.shycloud.mido.music.mapper.MisUserMusicMapper;
import com.shycloud.mido.music.service.MisMusicService;
import com.shycloud.mido.music.vo.MisMusicVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class MisMusicServiceImpl extends ServiceImpl<MisMusicMapper, MisMusic> implements MisMusicService {

	/**
	 * 曲目模块 mapper
	 */
	private MisMusicMapper musicMapper;
	/** 用户下载音乐模块 mapper */
	private MisUserMusicMapper userMusicMapper;

	/**
	 * 分页获取曲目信息
	 *
	 * @param page
	 * @param conllectionId
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<com.shycloud.mido.music.vo.MisMusicVO>
	 * @author nianhua.jiang
	 * @date 2020/6/12 16:17
	 */
	@Override
	public IPage<MisMusicVO> getMusicPage(Page page, Integer conllectionId) {

		if (SecurityUtils.getMember() != null) {
			return musicMapper.getMusicPage(page, conllectionId, SecurityUtils.getMember().getId());
		} else {
			return musicMapper.getMusicPage(page, conllectionId, "xxx");
		}

	}

	/**
	 * 判断该用户是否为vip用户
	 *
	 * @param memberId
	 * @return java.lang.String
	 * @author nianhua.jiang
	 * @date 2020/6/30 10:06
	 */
	@Override
	public String checkIsVip(String memberId) {
		return musicMapper.checkIsVip(memberId);
	}

	/**
	 * 添加曲目
	 *
	 * @param misMusic
	 * @return int
	 * @author nianhua.jiang
	 * @date 2020/7/2 17:37
	 */
	@Override
	public int addMusic(MisMusic misMusic) {

		//通过id获取music信息
		MisMusic music = musicMapper.selectById(misMusic.getId());

		//曲目名称
		music.setName(misMusic.getName());
		//所属曲集
		music.setCollectionId(misMusic.getCollectionId());
		//曲目排序
		music.setSort(misMusic.getSort());
		//曲目权限（0：普通用户 1.VIP用户）
		music.setMemberAuthority(misMusic.getMemberAuthority());
		music.setKalaSpeed(misMusic.getKalaSpeed());
		music.setKalaStartTime(misMusic.getKalaStartTime());

		return musicMapper.updateById(music);
	}

	/**
	 * 获取曲目信息
	 *
	 * @param page
	 * @param name
	 * @param status
	 * @param memberAuthority
	 * @param id
	 * @param collectionId
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<com.shycloud.mido.music.domain.MisMusic>
	 * @author nianhua.jiang
	 * @date 2020/7/3 9:48
	 */
	@Override
	public IPage<MisMusic> getMusicList(Page page, String name, String status, String memberAuthority, Integer id, Integer collectionId) {
		return musicMapper.getMusicList(page, name, status, memberAuthority, id, collectionId);
	}

	/**
	 * 书籍配套资源
	 *
	 * @param
	 * @return java.lang.Integer
	 * @author nianhua.jiang
	 * @date 2020/7/3 14:57
	 */
	@Override
	public List<MisMusic> getResouresCount() {
		return musicMapper.getResouresCount();
	}

	/**
	 * 更新下载次数
	 *
	 * @return int
	 * @author nianhua.jiang
	 * @date 2020/9/14 17:36
	 **/
	@Override
	public int updateDownLoadMp3Count() {
		return musicMapper.updateDownLoadMp3Count();
	}


	@Override
	public MisMusic getValidMusicById(Integer musicId) {
		return musicMapper.getValidMusicById(musicId);
	}

	/**
	 * 更新MP3下载次数
	 *
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2020/9/1 2:13
	 **/
	@Override
	public boolean downloadMp3Success() {

		//下载成功，累加下载次数
		int downloadMp3Count = userMusicMapper.getDownloadMp3Count(SecurityUtils.getMember().getId());
		//TODO 暂定下载一次请求一次接口，以后会确认具体操作流程
		int downloadMp3CountAfter = downloadMp3Count + 1;
		//更新
		return userMusicMapper.updateDownloadMp3Count(downloadMp3CountAfter, SecurityUtils.getMember().getId()) == 1;
	}

	@Override
	public String getIsTeacher(String memberId) {
		return musicMapper.getIsTeacher(memberId);
	}
}
